package com.douqu.game.core.config.mall;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;

/**
* Created by ${ZhaoLong} on 2018/3/28.
*/
public class MallConfig extends GameObject {

    public String sortShow;
    /**
     * 商店类型
     */
    public int mallType;

    /**
     * 页签类型
     */
    public int sortType;

    /**
     * 页签名称
     */
    public String sortName;

    /**
     * 商品
     */
    public GoodsData goods;

    /**
     * 原价格
     */
    public GoodsData[] originalPrice;

    /**
     * 折扣价
     */
    public GoodsData[] discountPrice;

    /**
     * 折扣
     */
    public String discount;

    /**
     * 限购数量
     */
    public int RestrictionAmount;

    /**
     * vip 限制条件
     */
    public int minLevel;


    @Override
    public boolean check() {
        if (goods == null) {
            LogUtils.error("MallConfig check goods is null -> id:" + id + " name:" + name);
            return false;
        }

        if (originalPrice == null) {
            LogUtils.error("MallConfig check upCri is null -> id:" + id + " name:" + name);
            return false;
        }

        if (discountPrice == null) {
            LogUtils.error("MallConfig check upCri is null -> id:" + id + " name:" + name);
            return false;
        }
        return true;
    }

    @Override
    public boolean setVariable(String key, String value) {
        if ("goods".equals(key)) {
            goods = new GoodsData(value);
            return goods.check(this.getClass(), key, id);
        } else if ("originalPrice".equals(key)) {
            originalPrice = LoadUtils.loadGoodDataToArray(key, value);
//            GoodsData[] checkData = new GoodsData[originalPrice.length];
//            for (int i = 0; i < originalPrice.length; i++) {
//                checkData[i] = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE,
//                        originalPrice[i].id, originalPrice[i].value);
//            }
            return LoadUtils.checkGoods(originalPrice, this.getClass(), key, id);
        } else if ("discountPrice".equals(key)) {
            discountPrice = LoadUtils.loadGoodDataToArray(key, value);
//            GoodsData[] checkData = new GoodsData[discountPrice.length];
//            for (int i = 0; i < discountPrice.length; i++) {
//                checkData[i] = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE,
//                        discountPrice[i].id, discountPrice[i].value);
//            }
            return LoadUtils.checkGoods(discountPrice, this.getClass(), key, id);
        } else {
            return super.setVariable(key, value);
        }
    }

    @Override
    public String toString() {
        return "MallConfig{" +
                "mallType=" + mallType +
                ", sortType=" + sortType +
                ", sortName='" + sortName + '\'' +
                ", goods=" + goods +
                ", originalPrice=" + Arrays.toString(originalPrice) +
                ", discountPrice=" + Arrays.toString(discountPrice) +
                ", discount='" + discount + '\'' +
                ", RestrictionAmount=" + RestrictionAmount +
                ", minLevel=" + minLevel +
                '}' + super.toString();
    }
}
